home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!sun1!ua302aa
- From: ua302aa@sun1.lrz-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: How to dynamically allocate first element of linked list.
- Date: 11 Jan 1996 13:19:58 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4d32pu$e8g@sparcserver.lrz-muenchen.de>
- References: <erkDL007G.rt@netcom.com>
- NNTP-Posting-Host: sun1.lrz-muenchen.de
-
- erk@netcom.com (Staugher) writes:
-
- >Im trying (without any luck )to write a function to allocate the first element
- >of a doubly linked list in C++. Unfortunately I learned data structures in
- >Pascal and have to port my knowledge (or lack of ) to C.
-
- >Here is my arrangement:
-
-
- >struct elmnt
- > { char name[20];
- > char number[17];
- > struct elmnt *forward_pointer;
- > struct elmnt *reverse_pointer;
- > }
-
- This is the first obvious problem with your code. Not reminating the
- definition of "struct emnt" with a semicolon defines main to return
- a "struct elmt".
-
- Avoiding implicit "int" functions helps to protect you from this
- type of error.
-
- >//======================================
-
- This is the second obvious problem. In C, "//" is a syntax error,
- not a comment delimiter.
-
- >main()
- >{
- >struct elmnt *list = newlist(void)
-
- This is the third obvious problem. You cannot use "void" the way you
- do. Change this to
-
- struct elmnt *list = newlist();
-
- >}
-
- >//======================================
-
- >struct elmnt * newlist(void)
- >{
- >struct elmnt *newptr;
- >newptr = malloc(sizeof(struct elmnt));
-
- For all we know, malloc() is an external function returning int and
- taking one argument of type size_t. While this second assuption
- is correct, the first one is wrong and can lead to unexpected results
- in some environments.
-
- >return (newptr);
- >}
-
- >//=======================================
-
-
- >The compiler complains with an ERROR that it cannot convert
- >(void *) to (elmnt *) in function newlist
-
- If you compile this code and the only diagnostic is that a "void *"
- cannot be converted to a "elmnt *", talk to your compiler vendor.
-
- In the posted piece of code, there is no type "elmnt", so it is
- pretty obvious that there cannot be a pointer to that type.
-
- Kurt
-
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-